package ute.mvc.controller.portal.admin;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import ute.mvc.model.Course;
import ute.mvc.model.OpenCourse;
import ute.mvc.model.Subject;
import ute.mvc.model.User;
import ute.mvc.service.CourseService;
import ute.mvc.service.OpenCourseService;
import ute.mvc.util.JsonObject;

@Controller
@RequestMapping(value = { "/portal/course" })
public class CourseController {

	@Autowired
	CourseService courseService;
	@Autowired
	OpenCourseService openCourseService;
	
	private static String course_Id = "";
	
	/**
	 * Get all courses from database
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String getAllCourse(Model model) {

		return "course_list";
	}
	
	/**
	 * Add new course
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAddCourse(Model model) {

		model.addAttribute("course", new Course());
		return "course_add";
	}
	
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public String addCourse(@Valid @ModelAttribute Course course, BindingResult result, Model model){
		
		if(result.hasErrors()) {
			return "course_add";
		}
		
		Course existCourse = courseService.getCourseById(course.getCourseId());
		if(existCourse != null) {
			result.rejectValue("courseId", "courseIdExist");
			return "course_add";
		}
		
		courseService.addCourse(course);
		return "redirect:list";
	}

	/**
	 * Edit course information
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="/detail/{courseId}", method = RequestMethod.GET)
	public String getCourseDetail(@PathVariable String courseId, Model model) {
		course_Id = courseId;
		
		model.addAttribute("course", courseService.getCourseById(courseId));
		return "course_detail";
	}
	
	@RequestMapping(value = "/edit/{courseId}", method = RequestMethod.GET)
	public String getEditCourse(@PathVariable String courseId, Model model) {
		
		model.addAttribute("course", courseService.getCourseById(courseId));
		return "course_edit";
	}
	
	@RequestMapping(value = "/edit/{courseId}", method = RequestMethod.POST)
	public String editCourse(@PathVariable String courseId, @Valid @ModelAttribute Course course,
			BindingResult result, Model model) {
		if(result.hasErrors())
			return "course_edit";
		
		courseService.updateCourse(course);
		return "redirect:../list";
	}
	
	@RequestMapping(value="/add-open-course/{courseId}", method=RequestMethod.GET)
	public String getAddOpenCourse(@PathVariable String courseId,@ModelAttribute OpenCourse openCourse,
			BindingResult result, Model model) {
		
		model.addAttribute("openCourse", new OpenCourse());
		model.addAttribute("courseId", courseId);
		return "course_add_opencourse";
	}
	
	@RequestMapping(value="/add-open-course/{courseId}", method=RequestMethod.POST)
	public String addOpenCourse(@PathVariable String courseId, @Valid @ModelAttribute OpenCourse openCourse,
			BindingResult result, Model model) {
		
		if(result.hasErrors())
			return "course_add_opencourse";
		
		if(openCourseService.getOpenCourseById(openCourse.getOpenCourseId()) != null ) {
			result.rejectValue("openCourseId", "openCourseIdExist");
			return "course_add_opencourse";
		}
		
		Course course = courseService.getCourseById(courseId);
		openCourse.setCourse(course);
		openCourseService.addOpenCourse(openCourse);
		
		return "redirect:../detail/" + courseId;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping("/listJSON")
	public JsonObject getAllCourseJSON() {

		List<Course> list = new ArrayList<Course>();
		list = courseService.getAllCourse();
		
		JsonObject subjectJson = new JsonObject();
		subjectJson.setAaData(list);
		subjectJson.setsEcho(1);
		subjectJson.setiTotalDisplayRecords(list.size());
		subjectJson.setiTotalRecords(list.size());
		return subjectJson;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping("/listOpenCourseJSON")
	public JsonObject getAllOpenCourseJSON() {
		List<OpenCourse> list = new ArrayList<OpenCourse>();
		
		//list = (List<OpenCourse>) courseService.getCourseById(course_Id).getListOpenCourse();
		list = courseService.getOpenCourseById(course_Id);
		
		JsonObject subjectJson = new JsonObject();
		subjectJson.setAaData(list);
		subjectJson.setsEcho(1);
		subjectJson.setiTotalDisplayRecords(list.size());
		subjectJson.setiTotalRecords(list.size());
		return subjectJson;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteCourse(@RequestBody String courses) {
		String[] courseIds = courses.split(";");
		if(courseService.deleteCourses(courseIds)) {
			return "success";
		} else {
			return "fail";
		}
	}
	
	@ResponseBody
	@RequestMapping("detail/delete")
	public String deleteOpenCourse(@RequestBody String openCourses) {
		String[] openCourseIds = openCourses.split(";");
		if(openCourseService.deleteOpenCourses(openCourseIds)) {
			return "success";
		} else {
			return "fail";
		}
	}
}
